#include "stdafx.h"

namespace hc
{
	FolderChecker::FolderChecker(const string& path):m_path(path)
	{
		Init();
	}
		
	bool FolderChecker::Check()
	{
		if (boost::filesystem::is_directory(m_path))
		{
			for (boost::filesystem::directory_iterator itr(m_path); itr!=boost::filesystem::directory_iterator(); ++itr)
			{
				if ( boost::filesystem::is_regular_file( itr->path() ) )
					m_Files.push_back( itr->path() ); 
			}
			Init();
			return true;
		}
		else 
			return boost::filesystem::exists( m_path );
	}

	int FolderChecker::GetResultSize()
	{
		return m_Files.size();
	}
			
	bool FolderChecker::MoveNext()
	{
		if ( m_itrFiles != m_Files.end() )
		{
			if ( ++m_itrFiles != m_Files.end() )
				return true;
		}
		return false;
	}

	boost::filesystem::path FolderChecker::GetCurrent()
	{
		return (*m_itrFiles);
	}

	void FolderChecker::Reset()
	{
		Init();
	}
	void FolderChecker::Init()
	{
		m_itrFiles = m_Files.begin();
	}

	list<boost::filesystem::path> FolderChecker::Filter(const set<string>& extensions )
	{
		list<boost::filesystem::path> m_filteredFilesPath;

		list< boost::filesystem::path >::iterator m_itFiles = m_Files.begin();
		list< boost::filesystem::path >::iterator m_itFiles_end = m_Files.end();
		for(; m_itFiles != m_itFiles_end; ++m_itFiles)
		{
			string _tmp = m_itFiles->extension().string();
		
			string _ext( ++_tmp.begin(), _tmp.end() );
			if( extensions.find( _ext ) != extensions.end() )
				m_filteredFilesPath.push_back( *m_itFiles );
		}
		return m_filteredFilesPath;
	}
};